
const CryptoJS = require('../../utils/crypto-js.js')

Page({
  data: {
    itrahost:"https://itra.run",
    triggered:false,
    areas:['全世界跑者','中国跑者'],
    genders:['所有性别','男','女'],
    genderIndex:0,
    areaIndex:0,
    toView: '',
    loading: false,
    noMore: false,
    session:'',
    application:'',
    list:[]
  },
  
  onLoad:function(){
    this.setData({
      triggered:true
    })
  },

  runnerinfo:function(e){
    var that = this;
    const app = getApp();
    wx.request({
      url: app.globalData.itra_info,
      method:"GET",
      success:(res)=>{
        var response = res.data;
        console.log(response);
        var session = response.Session;
        var application = response.Application;
        that.setData({
          session:session,
          application:application,
          list:[],
          triggered:true
        })
        that.loadData();
      },
      fail: (err) => {
        wx.showModal({
          title: '温馨提示',
          content: "网络较慢,数据请求失败!",
          showCancel:false,
          confirmText:"确定",
          confirmColor:"#000000"
        })
      }
    })
},

  onFressh:function(){
    this.setData({
      list:[]
    })
    if(this.data.session==''|| this.data.application ==''){
      this.runnerinfo();
    }else{
      this.loadData();
    }
  },
  loadData:function(){
      const app = getApp();
      const gen = this.data.genderIndex;
      const gender = ['','H','F'][gen];
      const araa = this.data.areaIndex;
      const countryCode = ['','CHN'][araa];
      wx.request({
        url: app.globalData.runnerRankUrl,
        data:{
          "start":this.data.list.length+1,
          "gender":gender,
          "countryCode":countryCode,
          "distanceId":'0',
          "pageNum":'0',
          "pageSize":"20",
          "ageGroup":""
        },
        header:{
          "Accept-Encoding": "gzip, deflate, br",
          "Accept": "*/*",
          "Content-Type":"application/json; charset=UTF-8",
          "Connection": "keep-alive",
          "Authorization":"eyJhbGciOiJIUzI1NiJ9.MjE4Mw.qpV884RGrJWf9NEcsOMXZ-d1NBEO85PlkcMqdvY1ewg",
          'Cookie': '.AspNetCore.Session='+this.data.session+'; .AspNetCore.Identity.Application='+this.data.application
        },
        method:"POST",
        success:(res)=>{
          var data = res.data;
          console.log(data);
          // var ciphertext_b64 = results.response1;
        if(data.code==0){
          // var iv_b64 = results.response2;
          // var key_b64 = results.response3;
          // const key = CryptoJS.enc.Base64.parse(key_b64);
          // const iv = CryptoJS.enc.Base64.parse(iv_b64);
          // const ciphertext = CryptoJS.enc.Base64.parse(ciphertext_b64);
          // const decrypted = CryptoJS.AES.decrypt(
          //     { ciphertext: ciphertext },
          //     key,
          //     {
          //       iv: iv,
          //       mode: CryptoJS.mode.CBC,
          //       padding: CryptoJS.pad.Pkcs7
          //     }
          //   );
          // const plaintext = decrypted.toString(CryptoJS.enc.Utf8);
          // // console.log('--->'+plaintext)
          const newItems = data.data;//JSON.parse(plaintext);
            // if (newItems.length > 0) {
              this.setData({
                list:newItems,
                loading:false,
                noMore: true,
                triggered:false
              })
            // } else {
            //   this.setData({ 
            //     noMore: true,
            //     triggered:false
            //   });
            // }
            
        }

        },
        fail: (err) => {//请求失败
          this.setData({
            triggered:false
          })
        }
      })
  },
  onItem:function(e){
    var index = e.currentTarget.id;
    var obj = this.data.list[index];
    console.log(obj);
    var info = {
      'runnerId':obj.runnerId,
      'firstName':obj.firstName,
      'lastName':obj.lastName,
      'gender':obj.gender,
      'pi':obj.pi,
      'nationality':this.data.itrahost+obj.countryCode,
      'profilePic':this.data.itrahost+obj.profilePic
    };
    var str = encodeURIComponent(JSON.stringify(info));
    wx.navigateTo({
      url: '../mine/mine?str='+str
    })
  },
  onCitySort(){
    var that = this;
    wx.showActionSheet({
      itemList: this.data.areas,
      success (res) {
        console.log(res.tapIndex)
        that.setData({
            areaIndex:res.tapIndex,
            triggered:true,
            toView:'top'
        })
      },
    })
  },
  onGenderSort(){
    var that = this;
    wx.showActionSheet({
      itemList: this.data.genders,
      success (res) {
        console.log(res.tapIndex)
        that.setData({
            genderIndex:res.tapIndex,
            triggered:true,
            toView:'top'
        })
      },
      fail (res) {
      }
    })
  },
  onShareAppMessage:function(){
    return {
        title: "ITRA积分排行榜"
    };
  },
  onShareTimeline:function(){
    return {
      title: "ITRA中国越野表现分排行榜"
    };
  },
  onScrollToLower: function() {
    if(this.data.list.length>=100) {
      this.setData({
        loading:false,
        triggered:false,
        noMore:true
      })
      return;
    }
    this.setData({
      loading:true,
      triggered:false
    })
    this.loadData();
  }
})
